package org.jeecg.modules.classrooms.classrooms.service.impl;


import org.jeecg.modules.classrooms.classrooms.entity.Classrooms;
import org.jeecg.modules.classrooms.classrooms.entity.ClassroomsProperty;
import org.jeecg.modules.classrooms.classrooms.mapper.ClassroomsMapper;
import org.jeecg.modules.classrooms.classrooms.mapper.ClassroomsPropertyMapper;
import org.jeecg.modules.classrooms.classrooms.service.IClassroomsService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: classrooms
 * @Author: jeecg-boot
 * @Date:   2020-06-03
 * @Version: V1.0
 */
@Service
public class ClassroomsServiceImpl extends ServiceImpl<ClassroomsMapper, Classrooms> implements IClassroomsService {

	@Autowired
	private ClassroomsMapper classroomsMapper;
	@Autowired
	private ClassroomsPropertyMapper classroomsPropertyMapper;
	
	@Override
	@Transactional
	public void saveMain(Classrooms classrooms, List<ClassroomsProperty> classroomsPropertyList) {
		classroomsMapper.insert(classrooms);
		if(classroomsPropertyList!=null && classroomsPropertyList.size()>0) {
			for(ClassroomsProperty entity:classroomsPropertyList) {
				//外键设置
				entity.setClassroomsId(classrooms.getId());
				classroomsPropertyMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(Classrooms classrooms,List<ClassroomsProperty> classroomsPropertyList) {
		classroomsMapper.updateById(classrooms);
		
		//1.先删除子表数据
		classroomsPropertyMapper.deleteByMainId(classrooms.getId());
		
		//2.子表数据重新插入
		if(classroomsPropertyList!=null && classroomsPropertyList.size()>0) {
			for(ClassroomsProperty entity:classroomsPropertyList) {
				//外键设置
				entity.setClassroomsId(classrooms.getId());
				classroomsPropertyMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		classroomsPropertyMapper.deleteByMainId(id);
		classroomsMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			classroomsPropertyMapper.deleteByMainId(id.toString());
			classroomsMapper.deleteById(id);
		}
	}
	
}
